home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: division problem
- Date: 29 Jan 1996 17:43:05 GMT
- Organization: OpenVision
- Message-ID: <4ej0v9$rfo@spanky.pls.ov.com>
- References: <31097D77.11AA@rain.org>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article 11AA@rain.org, tpaul <tpaul@rain.org> writes:
- >Can anyone show me why this does not work? I am a beginner.
- >
- >#include <stdio.h>
- >
- >main ()
- >{
- > int fahrenheit, celsius;
- >
- > printf("Fahrenheit temperature =?";
- > scanf("%d",fahrenheit);
- > celsius = 5/9*(fahrenheit-32);
- > printf("Fahrenheit = %d, Celsius = %d", fahrenheit, celsius;
- >}
- >
- >
- >Thanks in advance.
-
-
- First, * and / have the same precedence, so in all probability, the
- expression in the parens is evaluated first, then you get the rest of
- the expression.
-
- Second, integer divide truncates. Therefore 5/9 = 0. If you want to
- stick to integers, then write the following expression:
-
- celsius = (5 * (fahrenheit - 32)) / 9;
-
- This expression will give you a better answer, but not as good an
- answer as you would get from using floating point numbers.
-
- Fletcher.Glenn@ov.com
-
-